package com.lemon.library.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.ActionBar;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.library.R;
import com.lemon.library.mvp.BaseView;
import com.lemon.library.utils.StatusBarUtil;
import com.lemon.library.utils.ToastUtils;
import com.lemon.library.view.dialog.ProgressDialog;
import com.lemon.library.view.swipebacklayout.SwipeBackActivity;

/**
 * Created by Wisn on 2018/5/4 上午9:02.
 */

public abstract class BaseActivity extends SwipeBackActivity implements BaseView, View.OnClickListener {
    public static String Index_Tag = "Index_Tag_activity";
    private boolean isFrist = true;

    protected TextView backView;
    protected TextView contentView;
    protected TextView rightView;

    public ProgressDialog progressDialog;
    private int progressDialogCount;

    private final String mDotTag = this.getClass().getSimpleName();

    private static Application.ActivityLifecycleCallbacks mLifecycleCallbacks;
    public View netWork;
    public FrameLayout rootDecodeView;
    private ImageView ivClose;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideActionBar();
        setContentView(bindLayout());
        initTitle();
        initView(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    /**
     *
     * @return
     */
    protected boolean setStatusBar() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.theme_color), 0);
        return true;
    }

    protected void initTitle() {
        backView = (TextView) findViewById(R.id.common_back_iv);
        if (backView != null) {
            backView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        }
        contentView = (TextView) findViewById(R.id.common_content_tv);
        rightView = (TextView) findViewById(R.id.common_right_tv);

        ivClose = (ImageView) findViewById(R.id.iv_close);
        if (ivClose != null){
            ivClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    public void showCloseButton(){
        if (ivClose != null){
            ivClose.setVisibility(View.VISIBLE);
        }
    }

    public void setTitleText(String title) {
        if (contentView != null) {
            contentView.setText(title);
        }
    }

    public void hideActionBar() {
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.hide();
        }
    }

    public void showActionBar() {
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.show();
        }
    }


    public abstract int bindLayout();

    public abstract void initView(Activity activity);

    public abstract void initData(Context context);


    /**
     * 显示载入框
     *
     * @param msg
     */
    public void showLoading(String msg) {
        try {
            if (progressDialogCount <= 0 || progressDialog == null) {
                if (progressDialog == null) {
                    progressDialog = ProgressDialog.getInstance(msg);
                }
                if (progressDialog != null && !isFinishing()) {
                    progressDialog.show(getSupportFragmentManager(), progressDialog.getTag());
                }
            }
            progressDialogCount++;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showLoading() {
        showLoading(getString(R.string.loading_waiting));
    }

    /**
     * 取消载入框
     */
    public void hideLoading() {
        if (--progressDialogCount <= 0) {
            progressDialogCount = 0;
            if (progressDialog != null && !progressDialog.isHidden() && !isFinishing()) {
                progressDialog.dismiss();
                progressDialog.dismissAllowingStateLoss();
            }
        }
    }



    @Override
    protected void onStart() {
        super.onStart();
        if (isFrist) {
            initData(this);
            isFrist = false;
        }
    }


    @Override
    public void onClick(View v) {
        if (v == netWork) {
            startActivity(new Intent(Settings.ACTION_SETTINGS));
        }
    }

    @Override
    public void onNetStart(String tag, String startMsg) {

    }

    @Override
    public void onNetError(String tag, String errorMsg) {
        hideLoading();
        if (!TextUtils.isEmpty(errorMsg)) {
            ToastUtils.show(errorMsg);
        }
    }


    @Override
    public void onNetFinish(String tag, String startMsg) {
        hideLoading();
    }

    @Override
    public void onBackPressed() {
        if (!isFinishing()) {
            super.onBackPressed();
        }
    }


}
